package io.renren.modules.project.controller;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.renren.common.annotation.SysLog;
import io.renren.common.exception.RRException;
import io.renren.common.utils.RandomId;
import io.renren.modules.oss.service.FileUpLoadSrv;
import io.renren.modules.project.entity.ConferenceAccessoryEntity;
import io.renren.modules.project.service.ConferenceAccessoryService;
import io.renren.modules.project.service.PojectOperationService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.project.entity.ConferenceSummaryEntity;
import io.renren.modules.project.service.ConferenceSummaryService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;


/**
 * 
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-10-05 18:16:20
 */
@RestController
@RequestMapping("/generator/conferencesummary")
public class ConferenceSummaryController {
	@Autowired
	private ConferenceSummaryService conferenceSummaryService;
	@Autowired
	private FileUpLoadSrv fileUpLoadSrv;
	@Autowired
	private ConferenceAccessoryService conferenceAccessoryService;
	
	/**
	 * 根据项目ID查询会议信息
	 */
	@RequestMapping("/queryProjectIdList")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<ConferenceSummaryEntity> conferenceSummaryList = conferenceSummaryService.queryList(query);
		int total = conferenceSummaryService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(conferenceSummaryList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("generator:conferencesummary:info")
	public R info(@PathVariable("id") String id){
		ConferenceSummaryEntity conferenceSummary = conferenceSummaryService.queryObject(id);
		
		return R.ok().put("conferenceSummary", conferenceSummary);
	}

	/*
	@RequestMapping("/queryProjectIdList/{projectId}")
	public R queryProjectIdList(@PathVariable("projectId") String projectId){
		List<ConferenceSummaryEntity> conferenceSummaryList = conferenceSummaryService.queryProjectIdList(projectId);
		return R.ok().put("conferenceSummary", conferenceSummaryList);
	}*/
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("generator:conferencesummary:save")
	public R save(@RequestBody ConferenceSummaryEntity conferenceSummary){
		conferenceSummaryService.save(conferenceSummary);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@SysLog("修改会议纪要")
	@RequestMapping("/update")
	@RequiresPermissions("generator:conferencesummary:update")
	public R update(@RequestBody ConferenceSummaryEntity conferenceSummary){
		conferenceSummaryService.update(conferenceSummary);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("generator:conferencesummary:delete")
	public R delete(@RequestBody String[] ids){
		conferenceSummaryService.deleteBatch(ids);
		
		return R.ok();
	}

	/**
	 * 新增会议纪要
	 */
	@SysLog("新增会议纪要")
	@RequestMapping("/addConferenceSummary")
	public R addConferenceSummary(@RequestBody ConferenceSummaryEntity conferenceSummary){
		conferenceSummaryService.addConferenceSummary(conferenceSummary);
		return R.ok();
	}
	//查询详情
	@RequestMapping("/queryDetailsMeeting/{id}")
	public R queryDetailsMeeting(@PathVariable("id") String id,HttpServletRequest request){
		ConferenceAccessoryEntity conferenceAccessory = conferenceSummaryService.queryDetailsMeeting(id,request);
		return R.ok().put("conferenceAccessory",conferenceAccessory);
	}

	//首页渲染会议通知
	@RequestMapping("/mainQueryConferenceSummary/{userId}")
	public R mainQueryConferenceSummary(@PathVariable("userId") String userId){
        List<ConferenceSummaryEntity> summaryEntitie = conferenceSummaryService.mainQueryConferenceSummary(userId);
        R r = new R();
        r.put("summaryList",summaryEntitie);
        r.put("size",summaryEntitie.size());
		return r;
	}


	/**
	 * @param file 上传的文件
	 * 任务工作项附件上传
	 */
	@SysLog("新增会议纪要项附件")
	@RequestMapping("/upload")
	public R upload(@RequestParam("file") MultipartFile file, ConferenceAccessoryEntity conferenceAccessoryEntity, HttpServletRequest request) throws Exception {
		if (file.isEmpty()) {
			throw new RRException("上传文件不能为空");
		}
		String fileName = file.getOriginalFilename();	// 文件名
		Map<String, String> map = new HashMap<String, String>();
		try {
			map.putAll(fileUpLoadSrv.write(file, ""));
			conferenceAccessoryEntity.setId(RandomId.createId()+"");
			Date currentTime = new Date();
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String dateString = formatter.format(currentTime);
			conferenceAccessoryEntity.setCreationTime(dateString);
			conferenceAccessoryEntity.setMesgName(fileName);
			if ("1".equals(conferenceAccessoryEntity.getConferenceMesgAccessory())){
				conferenceAccessoryEntity.setConferenceAccessory("");
				conferenceAccessoryEntity.setConferenceMesgAccessory(map.get("filePath"));
			}else {
				conferenceAccessoryEntity.setConferenceMesgAccessory("");
				conferenceAccessoryEntity.setConferenceAccessory(map.get("filePath"));
			}
			conferenceAccessoryService.save(conferenceAccessoryEntity);
			return R.ok().put("success","上传成功");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return R.error().put("error",e.getMessage());
		} catch (IOException e) {
			e.printStackTrace();
			return R.error().put("error",e.getMessage());
		}
	}
}
